package com.ruoyi.forApp.service.imp;

import com.ruoyi.forApp.domain.NewsForApp;
import com.ruoyi.forApp.service.INewsServiceForApp;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.DeptServiceImpl;
import com.ruoyi.project.system.news.domain.News;
import com.ruoyi.project.system.news.service.NewsServiceImpl;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import com.ruoyi.project.system.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceForApp implements INewsServiceForApp {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private DeptServiceImpl deptService;
    @Autowired
    private NewsServiceImpl newsService;

    @Override
    public List<NewsForApp> selectNewsByUserId(Long userId) {
        User user=userService.selectUserById(userId);
        Dept dept=deptService.findSchoolObjByDeptId(user.getDeptId());
        List<News> newsList=newsService.selectNewsListByDeptId(dept.getDeptId());
        List<NewsForApp> newsForApps=new ArrayList<>();
        for (News news:newsList){
            NewsForApp newsForApp=new NewsForApp();
            newsForApp.setNewsId(news.getNewsId());
            newsForApp.setNewsTitle(news.getNewsTitle());
            newsForApp.setNewsContent(news.getNewsContent());
            if(!news.getImagePath().isEmpty()){
                newsForApp.setImagePath("/profile/upload/"+news.getImagePath());
            }
            newsForApp.setCreateBy(news.getCreateBy());
            newsForApp.setCreateTime(news.getCreateTime());
            newsForApps.add(newsForApp);
        }
        return newsForApps;
    }
}
